package jym.tan.tmdb.method.get;

import java.net.URL;

import jym.tan.tmdb.TMDB;
import jym.tan.tmdb.bean.wrapper.MovieSearchWrapper;
import jym.tan.tmdb.constant.TMDBConstants;
import jym.tan.tmdb.method.Base;

import org.apache.commons.lang.StringUtils;

/**
 * The Class SearchMovies. This class is similar to the <b>Search Movies</b> method described in 
 * <a href="http://help.themoviedb.org/kb/api/search-movies">Search Movies</a>.
 * This method is used to search movies in TMDb and iterate through the search results.
 *
 * @author Tanmoy
 */
public class SearchMovies extends Base {

	/**
	 * Instantiates a new SearchMovies.
	 *
	 * @param tmdb 
	 * 		An instance of the {@link TMDB} class
	 */
	public SearchMovies(TMDB tmdb) {
		super(tmdb, TMDBConstants.SEARCH_MOVIES);
	}

	/**
	 * Search for a movie with the help of given parameters and return a {@link MovieSearchWrapper} object.
	 *
	 * @param movieName 
	 * 		The movie name
	 * @param year 
	 * 		The movie released year to narrow the search results
	 * @param page 
	 * 		Some searches will have more than 20 results, the default 
	 * 		number of items returned per page. To iterate through use the page parameter
	 * @param allResults 
	 * 		Some searches will have more than 20 results. So the value 
	 * 		of <code>allResults</code> toggle whether or not the <code>MovieSearchWrapper</code> 
	 * 		object contains all search results 
	 * @param language
	 * 		The language parameter tries to localize the movie data we return. If the 
	 * 		language being requested does not exist, the value will be left blank. 
	 * 		The expected value is a ISO 639-1 code
	 * @param includeAdult 
	 * 		You can toggle whether or not to include adult items in 
	 * 		your search by using this parameter. The expected value is either true or false
	 * @return 
	 * 		The <code>MovieSearchWrapper</code> object
	 * @throws Exception 
	 * 		If any kind off exception occurred
	 */
	public MovieSearchWrapper search(String movieName, String year, int page, boolean allResults, String language, boolean includeAdult) throws Exception {
		String query = null;
		if (StringUtils.isNotBlank(year)) {
			query = new StringBuilder(movieName).append(" ").append(year).toString();
		} else {
			query = movieName;
		}

		URL searchURL = getUrl(query, TMDBConstants.BLANK, language, TMDBConstants.BLANK, page, includeAdult);
		MovieSearchWrapper wrapper = getJsonObject(searchURL, MovieSearchWrapper.class);
		return allResults ? search(wrapper, query, language, includeAdult) : wrapper;
	}

	/**
	 * Search for all results of a given movie search.
	 *
	 * @param wrapper 
	 * 		The <code>MovieSearchWrapper</code> object
	 * @param query 
	 * 		The query to be searched
	 * @param language 
	 * 		The language parameter tries to localize the movie data we return. If the 
	 * 		language being requested does not exist, the value will be left blank. 
	 * 		The expected value is a ISO 639-1 code
	 * @param includeAdult 
	 * 		You can toggle whether or not to include adult items in 
	 * 		your search by using this parameter. The expected value is either true or false
	 * @return 
	 * 		The <code>MovieSearchWrapper</code> object
	 * @throws Exception 
	 * 		If any kind off exception occurred
	 */
	private MovieSearchWrapper search(MovieSearchWrapper wrapper, String query, String language, boolean includeAdult) throws Exception {
		if (wrapper.getTotalPages() == 1) {
			return wrapper;
		}

		for (int i = 2; i <= wrapper.getTotalPages(); i++) {
			URL searchURL = getUrl(query, TMDBConstants.BLANK, language, TMDBConstants.BLANK, i, includeAdult);
			Thread.sleep(2000);
			MovieSearchWrapper movieSearchWrapper = getJsonObject(searchURL, MovieSearchWrapper.class);
			wrapper.getResults().addAll(movieSearchWrapper.getResults());
		}

		return wrapper;
	}
}
